#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int g_bowl = 1;

pthread_mutex_t g_lock;

void* EatStart(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&g_lock);
        printf("i am %p, i eat %d\n",pthread_self(),g_bowl);
        g_bowl--;
        pthread_mutex_unlock(&g_lock);
    }
    return NULL;
}

void* MakeStart(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&g_lock);
        g_bowl++;
        printf("i am %p, i make %d\n",pthread_self(),g_bowl);
        pthread_mutex_unlock(&g_lock);
    }
    return NULL;
}

int main()
{
    pthread_mutex_init(&g_lock,NULL);

    pthread_t tid;
    int ret = pthread_create(&tid,NULL,EatStart,NULL);
    if(ret < 0)
    {
        perror("pthread_create");
        return 0;
    }
    ret = pthread_create(&tid,NULL,MakeStart,NULL);
    if(ret < 0)
    {
        perror("pthread_create");
        return 0;
    }

    while(1)
    {
        sleep(1);
    }

    pthread_mutex_destroy(&g_lock);

    return 0;
}
